﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Logging;
using WuDada.Core.SystemApplications.Domain;
using System.Collections.Generic;
using WuDada.Core.Member;
using WuDada.Core.Member.Service;
using WuDada.Core.Member.Domain;
using WuDada.Core.Post.Domain;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;

public partial class admin_UC08_0811 : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private MemberFactory m_MemberFactory;
    private IMemberService m_MemberService;
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private WebLogService m_WebLogService;

    private int m_Mode
    {
        get { if (ViewState["mode"] == null) { ViewState["mode"] = 0; } return int.Parse(ViewState["mode"].ToString()); }
        set { ViewState["mode"] = value; }
    }
    private string m_PicFileName
    {
        get { if (ViewState["picfilename"] == null) { ViewState["picfilename"] = string.Empty; } return ViewState["picfilename"].ToString(); }
        set { ViewState["picfilename"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        m_WebLogService = new WebLogService();
        m_MemberFactory = new MemberFactory();
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_MemberService = m_MemberFactory.GetMemberService();

        if (!IsPostBack)
        {
            pnlContent.Visible = false;
            btnSearch_Click(null, null);
            ShowMode();
        }
    }

    private void ShowMode()
    {
        if (m_Mode == 0)
        {
            btnSave.Visible = false;
        }
        else
        {
            btnSave.Visible = true;
        }
    }

    private void fillGridView()
    {
        IDictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("Order", "order by m.CreatedDate desc ");

        if (!string.IsNullOrEmpty(ddlSearchStatus.SelectedValue))
        {
            conditions.Add("Status", ddlSearchStatus.SelectedValue);
        }

        if (!string.IsNullOrEmpty(txtKeyWord.Text.Trim()))
        {
            conditions.Add("KeyWord", txtKeyWord.Text.Trim());
        }

        //分頁
        AspNetPager1.RecordCount = m_MemberService.GetMemberCount(conditions);
        conditions.Add("PageIndex", (AspNetPager1.CurrentPageIndex - 1).ToString());
        conditions.Add("PageSize", AspNetPager1.PageSize.ToString());

        gvList.DataSource = m_MemberService.GetMemberList(conditions);
        gvList.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        fillGridView();
    }    

    private void ClearUI()
    {
        UIHelper.ClearUI(pnlContent);
        m_Mode = 0;
        m_PicFileName = string.Empty;
        ddlStatus.SelectedValue = string.Empty;
        ddlIsAdmin.SelectedValue = string.Empty;
        pnlContent.Visible = false;
    }

    protected void gvList_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        int memberId = int.Parse(e.CommandArgument.ToString());
        MemberVO memberVO = m_MemberService.GetMemberById(memberId);
        switch (cmdName)
        {
            case "myModify":
                ClearUI();
                UIHelper.FillUI(pnlContent, memberVO);
                m_Mode = memberId;
                lblCreateDate.Text = memberVO.CreatedDate.ToString();
                if (!string.IsNullOrEmpty(memberVO.HtmlContent))
                {
                    lblHtmlContent.Text = memberVO.HtmlContent.Replace(Environment.NewLine, "<br>");
                }
                ShowMode();
                pnlContent.Visible = true;
                btnSearch.Enabled = false;
                ddlSearchStatus.Enabled = false;
                txtKeyWord.Enabled = false;
                LoadSubList();
                break;
            case "myDel":
                m_MemberService.DeleteMember(memberVO);
                m_WebLogService.AddSystemLog(MsgVO.Action.刪除, memberVO);
                break;

            default:
                break;
        }
        btnSearch_Click(null, null);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ClearUI();
        ShowMode();
        btnSearch.Enabled = true;
        ddlSearchStatus.Enabled = true;
        txtKeyWord.Enabled = true;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        fillGridView();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        MemberVO memberVO = m_MemberService.GetMemberById(m_Mode);
        WebLogService.SetUpdateInfo(memberVO);
        memberVO.IsAdmin = bool.Parse(ddlIsAdmin.SelectedValue);
        memberVO.Status = ddlStatus.SelectedValue;
        m_MemberService.UpdateMember(memberVO);
        m_WebLogService.AddSystemLog(MsgVO.Action.修改, memberVO);
        fillGridView();
        ClearUI();
        ShowMode();
        btnSearch.Enabled = true;
        ddlSearchStatus.Enabled = true;
        txtKeyWord.Enabled = true;
    }

    protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    }

    protected void btnShowAdd_Click(object sender, EventArgs e)
    {
        ClearUI();
        m_PicFileName = string.Empty;
        ShowMode();
        pnlContent.Visible = true;
        btnSearch.Enabled = false;
        ddlSearchStatus.Enabled = false;
        txtKeyWord.Enabled = false;
    }

    protected void btnAddLearn_Click(object sender, EventArgs e)
    {
        PostVO postVO = new PostVO();
        postVO.Member = m_MemberService.GetMemberById(m_Mode);
        postVO.Title = txtLearnTitle.Text.Trim();
        postVO.CustomField1 = txtLearnDatePeriod.Text.Trim();
        postVO.CustomField2 = txtHours.Text.Trim();
        postVO.Flag = 1;
        WebLogService.SetCreateInfo(postVO);
        m_PostService.CreatePost(postVO);
        m_WebLogService.AddSystemLog(MsgVO.Action.新增, postVO);
        LoadSubList();
        UIHelper.ClearUI(PanelSub);
    }

    private void LoadSubList()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("MemberId", m_Mode.ToString());
        conditions.Add("Flag", "1");
        conditions.Add("Order", "order by p.CreatedDate desc ");

        //分頁
        AspNetPager2.RecordCount = m_PostService.GetPostCount(conditions);
        int pageIndex = (AspNetPager2.CurrentPageIndex - 1);
        int pageSize = AspNetPager2.PageSize;
        conditions.Add("PageIndex", pageIndex.ToString());
        conditions.Add("PageSize", pageSize.ToString());

        gvSubList.DataSource = m_PostService.GetPostList(conditions);
        gvSubList.DataBind();
    }

    protected void AspNetPager2_PageChanged(object sender, EventArgs e)
    {
        LoadSubList();
    }

    protected void gvSubList_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        int postId = int.Parse(e.CommandArgument.ToString());
        PostVO postVO = m_PostService.GetPostById(postId);
        switch (cmdName)
        {
            case "myDel":
                m_PostService.DeletePost(postVO);
                m_WebLogService.AddSystemLog(MsgVO.Action.刪除, postVO);
                break;

            default:
                break;
        }
        LoadSubList();
    }
}